package com.lzy.business.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.business.facade.ProductCouponFacade;
import com.lzy.business.request.ProductInfoRequest;
import com.lzy.business.response.ProductInfoResponse;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.common.util.UserUtil;
import com.lzy.product.dtos.CouponPriceDto;
import com.lzy.product.dtos.ProductInfoDto;
import com.lzy.product.dtos.ProductYieldRatioDto;
import com.lzy.product.dtos.ProjectDto;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by WIN7 on 2017/12/28.
 */
@Controller
@RequestMapping(value = "/admin/coupon")
public class ProductCouponController extends BaseController{

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductCouponController.class);

    @Autowired
    ProductCouponFacade productCouponFacade;

    /**
     * 卡券列表页面
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String list() {
        return "product/coupon/coupon_list";
    }

    /**
     * 查询列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public PageBean json(ProductInfoRequest request) {
        Ensure.that(request).isNotNull("07000001");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        PageInfo<ProductInfoDto> pageInfo = productCouponFacade.selectList(pager, DozerUtils.map(request, ProductInfoDto.class));
        if (null != pageInfo) {
            List<ProductInfoDto> list = pageInfo.getList();
            return new PageBean<ProductInfoResponse>(pageInfo.getTotal(), DozerUtils.maps(list, ProductInfoResponse.class));
        } else {
            return null;
        }
    }

    /**
     * 根据产品id获取卡券套餐返现比
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping(value = "/cashList", method = RequestMethod.GET)
    @ResponseBody
    public Result getCashBackRatioList(@RequestParam("productId") Long productId, Model model){
        Ensure.that(productId).isNotNull("07000001");
        List<ProductYieldRatioDto> productYieldRatioDtoList = productCouponFacade.getCashBackRatioList(productId);
        return new Result(productYieldRatioDtoList);
    }

    /**
     * 转向新增页面
     *
     * @return
     */
    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String toAdd(Model model) {
        List<ProjectDto> projectDtoList = productCouponFacade.toAdd();
        if(projectDtoList != null){
            model.addAttribute("couponList",projectDtoList);
        }
        List<CouponPriceDto> couponPriceDtoList = productCouponFacade.getCouponPriceDtoList();
        if(couponPriceDtoList != null){
            model.addAttribute("couponPriceList",couponPriceDtoList);
        }
        return "product/coupon/coupon_add";
    }

    /**
     * 保存新增卡券产品
     */
//    @RequiresPermissions("sys:coupon:add")
    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(ProductInfoRequest request) {
        Ensure.that(request).isNotNull("07000001");
        request.setCreateUser(UserUtil.getCurrentUser().getId());
        Boolean flag = productCouponFacade.insert(DozerUtils.map(request,ProductInfoDto.class));
        Ensure.that(flag).isTrue("07000002");
        return new Result();
    }

    /**
     * 转向编辑页面
     */
    @RequestMapping(value = "/edit")
    public String edit(@RequestParam("id") Long id, Model model) {
        Ensure.that(id).isNotNull("07000001");
        ProductInfoDto dto = productCouponFacade.toEdit(id);
        model.addAttribute("dto",dto);
        List<CouponPriceDto> couponPriceDtoList = productCouponFacade.getCouponPriceDtoList();
        if(couponPriceDtoList != null){
            model.addAttribute("couponPriceList",couponPriceDtoList);
        }
        return "product/coupon/coupon_add";
    }

    /**
     * 更新卡券产品
     */
//    @RequiresPermissions("sys:coupon:update")
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result update(ProductInfoRequest request) {
        Ensure.that(request).isNotNull("07000001");
        request.setUpdateUser(UserUtil.getCurrentUser().getId());
        Boolean flag = productCouponFacade.update(DozerUtils.map(request,ProductInfoDto.class));
        Ensure.that(flag).isTrue("07000002");
        return new Result();
    }

}
